﻿package fj.ui
{
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import fj.events.FJScrollbarEvent;
	import gs.TweenLite;

	public class FJScroll extends MovieClip
	{
		public var delay:Number;
		public var speed:Number;
		public var speedWheel:Number;

		private var _up:MovieClip;
		private var _down:MovieClip;
		private var _scroll:MovieClip;
		private var _conteudo:MovieClip;
		private var _mascara:MovieClip;
		private var _direction:String;
		private var _directionDown:Boolean;
		private var _percent:Number;

		public function FJScroll():void
		{
			delay = 0.5;
			speed = 5;
			speedWheel = 2;

			_up = getChildByName( "seta_u" ) as MovieClip;
			_down = getChildByName( "seta_d" ) as MovieClip;
			_scroll = getChildByName( "scrollBar" ) as MovieClip;
			_conteudo = getChildByName( "conteudo" ) as MovieClip;
			_mascara = getChildByName( "mascara" ) as MovieClip;

			if( _scroll ){
				_scroll.addEventListener( FJScrollbarEvent.SCROLL, scrollBarScroll );
				stage.addEventListener( MouseEvent.MOUSE_WHEEL, mouseWheel );
			} else {
				_percent = 0;
			}
			if ( _up ) {
				_up.mouseChildren = false;
				_up.buttonMode = true;
				_up.addEventListener( MouseEvent.MOUSE_DOWN, directionDown );
			}
			if ( _down ) {
				_down.mouseChildren = false;
				_down.buttonMode = true;
				_down.addEventListener( MouseEvent.MOUSE_DOWN, directionDown );
			}
			addEventListener( Event.ENTER_FRAME, loop );
		}
		private function scrollBarScroll( e:FJScrollbarEvent ):void
		{
			TweenLite.killTweensOf( _conteudo );
			TweenLite.to( _conteudo ,delay, {y:-e.percent*(_conteudo.height-_mascara.height)} );
		}
		private function directionDown( e:MouseEvent ):void
		{
			_directionDown = true;
			switch ( e.target ) {
				case _up :
					_direction = "up";
					break;
				case _down :
					_direction = "down";
					break;
			}
			stage.addEventListener( MouseEvent.MOUSE_UP, mouseUp );
		}
		private function loop( e:Event ):void
		{
			if ( _conteudo.height > _mascara.height ) {
				if ( _directionDown ) {
					switch ( _direction ) {
						case "up" :
							if( _scroll ){
								_scroll.scroll( -speed );
							} else {
								_percent += -speed/2/100;
								if( _percent < 0 ){
									_percent = 0;
								}
								TweenLite.killTweensOf( _conteudo );
								TweenLite.to( _conteudo ,delay, {y:-_percent*(_conteudo.height-_mascara.height)} );
							}
							break;
						case "down" :
							if( _scroll ){
								_scroll.scroll( speed );
							} else {
								_percent += speed/2/100;
								if( _percent > 1 ){
									_percent = 1;
								}
								TweenLite.killTweensOf( _conteudo );
								TweenLite.to( _conteudo, delay, {y:-_percent*(_conteudo.height-_mascara.height)} );
							}
							break;
					}
				}
				if ( _up ) {
					_up.visible = true;
				}
				if ( _down ) {
					_down.visible = true;
				}
				if( _scroll ){
					_scroll.visible = true;
				}
			} else {
				if ( _up ) {
					_up.visible = false;
				}
				if ( _down ) {
					_down.visible = false;
				}
				if( _scroll ){
					_scroll.visible = false;
				}
			}
		}
		private function mouseUp( e:MouseEvent ):void
		{
			_directionDown = false;
			stage.removeEventListener( MouseEvent.MOUSE_UP, mouseUp );
		}
		private function mouseWheel( e:MouseEvent ):void
		{
			_scroll.scroll(-e.delta*speed);
		}
	}
}